package com.wlps.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlps.contant.Contant;
import com.wlps.entity.WebLog;
import com.wlps.mapper.ClientMapper;
import com.wlps.mapper.OrderMapper;
import com.wlps.mapper.SysUserMapper;
import com.wlps.mapper.WebLogMapper;
import com.wlps.result.ResultVo;
import com.wlps.utils.JwtUtils;
import com.wlps.vo.IndexVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * <p>+
 *
 * </p>
 *
 * @author ZWYZY
 * @since 2020/8/28
 */
@RestController
@Slf4j
@RequestMapping("/api/index")
public class IndexController implements InitializingBean {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public void afterPropertiesSet() throws Exception {
        if (!redisTemplate.hasKey(Contant.ONLINEUSER_KEY)) {
            redisTemplate.opsForValue().set(Contant.ONLINEUSER_KEY, 0);
            log.info("初始化在线人数---------写入缓存");
        }
    }

    @GetMapping("/getData")
    public ResultVo<IndexVo> getData() {
        IndexVo indexVo = new IndexVo();
        indexVo.setOnlineusers((Integer) redisTemplate.opsForValue().get(Contant.ONLINEUSER_KEY));
        indexVo.setEmployeecount(sysUserMapper.selectCount(null));
        indexVo.setNumberofcustomers(clientMapper.selectCount(null));
        indexVo.setNumberoforders(orderMapper.selectCount(null));

        return new ResultVo<>("获取首页数据成功", 200, indexVo);
    }

    @Autowired
    WebLogMapper webLogMapper;

    @GetMapping("/getLogData")
    public ResultVo<List<WebLog>> getLogData() {
        QueryWrapper<WebLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(WebLog::getStartTime).last("limit 10");
        List<WebLog> webLogs = webLogMapper.selectList(queryWrapper);
        return new ResultVo<List<WebLog>>("请求成功", 200, webLogs);
    }



}
